home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / liveusb / source.py < prev    next >
Encoding:
Python Source  |  2012-02-24  |  3.6 KB  |  99 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. import os
  4. import shutil
  5. import sys
  6. import subprocess
  7. from stat import ST_SIZE
  8. from liveusb import _
  9. from liveusb.releases import releases
  10. from liveusb.utils import (_to_unicode, _dir_size, iso_is_live_system,
  11.                            unicode_to_utf8, _set_liberal_perms_recursive,
  12.                            underlying_physical_device)
  13.  
  14. class SourceError(Exception):
  15.     """ A generic error message that is thrown by the Source classes """
  16.  
  17. class Source(object):
  18.     def clone(self, destination):
  19.         raise NotImplementedError
  20.     def supports_verify_md5(self):
  21.         return False
  22.     def supports_verify_sha1(self):
  23.         return False
  24.  
  25. class LocalIsoSource(Source):
  26.     def supports_verify_md5(self):
  27.         return True
  28.     def supports_verify_sha1(self):
  29.         return True
  30.     def __init__(self, path):
  31.         self.path = os.path.abspath(_to_unicode(path))
  32.         self.size = os.stat(self.path)[ST_SIZE]
  33.         if not iso_is_live_system(self.path):
  34.             raise SourceError(_("Unable to find LiveOS on ISO"))
  35.         self.dev  = None
  36.         # This can fail for devices not supported by UDisks such as aufs mounts
  37.         try:
  38.             self.dev = underlying_physical_device(self.path)
  39.         except Exception, e:
  40.             print >> sys.stderr, _("Could not guess underlying block device: %s"
  41.                                    % e.args[0])
  42.             pass
  43.  
  44.     def clone(self, destination):
  45.         cmd = ['7z', 'x', unicode_to_utf8(self.path),
  46.                '-x![BOOT]', '-y', '-o%s' % (destination)]
  47.         proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
  48.                                 stderr=subprocess.PIPE)
  49.         out, err = proc.communicate()
  50.         if proc.returncode:
  51.             raise SourceError(_("There was a problem executing `%s`."
  52.                                 "%s\n%s" % (cmd, out, err)))
  53.         _set_liberal_perms_recursive(destination)
  54.  
  55.     def get_release(self):
  56.         """ If the ISO is for a known release, return it. """
  57.         isoname = os.path.basename(self.path)
  58.         for release in releases:
  59.             if os.path.basename(release['url']) == isoname:
  60.                 return release
  61.  
  62.     def verify_md5(self):
  63.         """ Verify the ISO md5sum.
  64.         At the moment this is Linux specific since checkisomd5 does not work
  65.         on Windows.
  66.         """
  67.         if sys.platform == 'win32':
  68.             return True
  69.         cmd = ['checkisomd5', unicode_to_utf8(self.path)]
  70.         proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
  71.                                 stderr=subprocess.PIPE)
  72.         out, err = proc.communicate()
  73.         if proc.returncode:
  74.             raise SourceError(_("ISO MD5 checksum verification failed:"
  75.                                 "%s\n%s" % (out, err)))
  76.         return True
  77.  
  78. class RunningLiveSystemSource(Source):
  79.     def __init__(self, path):
  80.         if not os.path.exists(path):
  81.             raise SourceError(_("'%s' does not exist") % path)
  82.         if not os.path.isdir(path):
  83.             raise SourceError(_("'%s' is not a directory") % path)
  84.         self.path = path
  85.         self.size = _dir_size(self.path)
  86.         self.dev  = underlying_physical_device(self.path)
  87.     def clone(self, destination):
  88.         for f in os.listdir(self.path):
  89.             src = os.path.join(self.path, f)
  90.             dst = os.path.join(destination, f)
  91.             if os.path.isfile(src):
  92.                 shutil.copy(src, dst)
  93.             elif os.path.islink(src):
  94.                 linkto = os.readlink(src)
  95.                 os.symlink(linkto, dst)
  96.             elif os.path.isdir(src):
  97.                 shutil.copytree(src, dst)
  98.         _set_liberal_perms_recursive(destination)
  99.